const app = getApp();
import provList from '../../utils/city.js'
Page({
  data: {
    cndUrl: app.domainName,
    provList,
    isX:'',
    userInfo: {prov:'',city:'',provid:'',cityid:''},
    username:'',
    address:'',
    tipNum:0,
    isLock:false,
    phone:'',
    opz:'opz',
    optionsShare:''
  },
  onLoad: function (options) {
    var _this=this;
    if(app.globalData.screenHeight > 1600){
      _this.setData({isX:'x-'});
     }
    if(app.globalData.entryIn == 'thinkhuinot'){
      app.globalData.entryIn='hashuinot'; 
    }
    if (typeof(options.share) != "undefined" && "goreg" != options.share){
      _this.setData({optionsShare: 'sharereg'});  
    }
    wx.request({ url: app.domainName+'web/?c=regphone',data: {o:app.globalData.openid},header: {'content-type': 'application/json' },
              success (res) {
            if('ok' == res.data.msg){
              _this.setData({phone:res.data.p});  
            }else{
              app.globalData.entryIn=''; 
              wx.redirectTo({url: '/pages/index/index'})
            }
           
            console.log(res.data)
     } });

  },
  bindchange(e) {
      let act = e.currentTarget.dataset.act
      let val = e.detail.value
     
      if (act == 'prov') {
          this.setData({
              'userInfo.prov': this.data.provList[val].name,
              'userInfo.provid': this.data.provList[val].id,
              'userInfo.city': '',
              cityList: this.data.provList[val].arr
          })
      } else if (act == 'city') {
          this.setData({
            'userInfo.city': this.data.cityList[val].name,
            'userInfo.cityid': this.data.cityList[val].id
        })
      }
  },
  getName(provid, cityid) {
    let data = this.data.provList
    let ret = {}
    data.map((v) => {
        if (v.id == provid) {
            ret.prov = v.name
            v.arr.map((n) => {
                if (n.id == cityid) {
                    ret.citys = n.name
                }
            })
        }
    })
    return ret
},
bindinput: function (e) {
  var _act=e.currentTarget.dataset.act;
  var _getvalue=e.detail.value;
  if('username' == _act){
    this.setData({ username: _getvalue})
  }
  if('addressTxt' == _act){
    this.setData({address: _getvalue})
  }
  console.log(_getvalue);
},
dozhuce: function () {
  if(this.data.isLock){return false;}
  var _this=this;
  var _name=this.data.username;
  var _userInfo=this.data.userInfo;
  var _address=this.data.address;
  var _opz=this.data.opz;
  console.log(_userInfo)
  if('' == _name){
    this.popMsg('请输入姓名');
    return;
  }
  if('' == _userInfo.prov){
    this.popMsg('请选择省');
    return;
  }
  if('' == _userInfo.city){
    this.popMsg('请选择市');
    return;
  }
  if('' == _address){
    this.popMsg('请输入详细地址');
    return;
  }
  if('' != _opz){
    this.popMsg('请同意隐私条款');
    return;
  }
  wx.showLoading({
    title: '会员注册中'
  });
  this.setData({ isLock: true});
  wx.request({ url: app.domainName+'web/?c=hui',
  method: 'POST',
  data: {
    n:_name,p:_userInfo.prov,city:_userInfo.city, 
    cityid: _userInfo.cityid, provid: _userInfo.provid,
    a:_address,o: app.globalData.openid},
    header: { 'content-type': 'application/x-www-form-urlencoded' },
    success (res) {
      if('ok' == res.data.s){
        if('' != app.globalData.entryIn){
           app.globalData.entryIn='sharereg'; 
        }
        wx.redirectTo({url: '/pages/index/index?isnew=ok'})
        return;
      }else{
        _this.setData({ isLock: false});
        _this.popMsg(res.data.m);
      }
      wx.hideLoading();
  }
 });
  
 app.linkflow.sendEvent({"event": "UDE_403MNDVF7KA","Click_register1": "立即注册提交按钮"})

},

popMsg: function (msg) {
    wx.showModal({content:msg,showCancel:false })
},

dothink: function () {
  if(app.globalData.entryIn == 'hashuinot'){
    app.globalData.entryIn='thinkhuinot';
  } 
  wx.redirectTo({url: '/pages/index/index'})
},
doclick: function (e) {
  var _id=e.currentTarget.dataset.id;
  if('okclick' == _id){
   
    var _opz=this.data.opz;
    console.log(_opz)
    if('opz' == _opz){
      this.setData({ opz: ''});
    }else{
      this.setData({ opz: 'opz'});
    }
    return;
  }
  
  if('ystkuan' == _id){
    this.setData({ tipNum: 3});
    return;
  }
  if('popclose' == _id){
    this.setData({ tipNum: 0});
    return;
  }
  if('chengget' == _id){
    wx.redirectTo({url: '/pages/group/index'})
    return;
  }
  if('chengchak' == _id){
    this.setData({ tipNum: 2});
    return;
  }
  if('popclose2' == _id){
    this.setData({ tipNum: 1});
    return;
  }
  if('youfuzhi' == _id){
    wx.setClipboardData({
      data: '￥VsW71xtQ4gB￥',
      success (res) {}})
    return;
  }
  if('youtu' == _id){
    this.saveImg(this.data.cndUrl+'img/reg/youhui-nx.png')
    return;
  }
},
saveImg(_url){
  let url = _url;
  //用户需要授权
  wx.getSetting({
    success: (res) => {
      if (!res.authSetting['scope.writePhotosAlbum']) {
        wx.authorize({
          scope: 'scope.writePhotosAlbum',
          success:()=> {
            // 同意授权
            this.saveImg1(url);
          },
          fail: (res) =>{
            console.log(res);
          }
        })
      }else{
        // 已经授权了
        this.saveImg1(url);
      }
    },
    fail: (res) =>{
      console.log(res);
    }
  })   
},
saveImg1(url){
  wx.getImageInfo({
    src: url,
    success:(res)=> {
      let path = res.path;
      wx.saveImageToPhotosAlbum({
        filePath:path,
        success:(res)=> { 
          console.log(res);
        },
        fail:(res)=>{
          console.log(res);
        }
      })
    },
    fail:(res)=> {
      console.log(res);
    }
  })
},


onHide: function () {

},
onShareAppMessage: function (res) {
  return {
    title: '联盟集结令，精致Girl都已经加入就差你了！',
    path: '/pages/index/index?share='+app.globalData.openid,
    imageUrl:'https://elixir.omnimkt.com/elixir07/img/64.png'
  }
}
})